package com.springdoor.wx.api.service.impl;

import com.springdoor.wx.api.entity.ResultCategory;
import com.springdoor.wx.api.mapper.SprdGoodsTypeMapper;
import com.springdoor.wx.api.service.SprdGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SprdGoodsTypeServiceImpl implements SprdGoodsTypeService {

    @Autowired
    private SprdGoodsTypeMapper goodsTypeMapper;

    @Override
    public List<ResultCategory> fetchCategory() {
        return goodsTypeMapper.fetchCategory();
    }

    @Override
    public List<ResultCategory> fetchCategoryTree() {
        return buildAttrTree(goodsTypeMapper.fetchCategoryTree());
    }

    private List<ResultCategory> buildAttrTree(List<ResultCategory> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<ResultCategory> treeList = new ArrayList<ResultCategory>();
        List<Long> tempList = list.stream().map(ResultCategory::getGroupId).collect(Collectors.toList());
        for (Iterator<ResultCategory> iterator = list.iterator(); iterator.hasNext();)
        {
            ResultCategory type = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId()))
            {
                recursionFn(list, type);
                treeList.add(type);
            }
        }
        if (treeList.isEmpty())
        {
            treeList = list;
        }
        List<ResultCategory> resultList = new ArrayList<ResultCategory>();
        treeList.forEach(e -> {
            if (e.getChildren() == null || e.getChildren().size() == 0) {
                List<ResultCategory> children = new ArrayList<>();
                children.add(copyEntity(e));
                e.setChildren(children);
            }
            ResultCategory category = copyEntity(e);
            List<ResultCategory> children = new ArrayList<>();
            children.add(e);
            category.setChildren(children);
            resultList.add(category);
        });
        return resultList;
    }

    private ResultCategory copyEntity(ResultCategory entity) {
        ResultCategory category = new ResultCategory();
        category.setGroupId(entity.getGroupId());
        category.setName(entity.getName());
        category.setThumbnail(entity.getThumbnail());
        return category;
    }

    private void recursionFn(List<ResultCategory> list, ResultCategory t) {
        // 得到子节点列表
        List<ResultCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ResultCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    private boolean hasChild(List<ResultCategory> list, ResultCategory t) {
        return getChildList(list, t).size() > 0;
    }

    private List<ResultCategory> getChildList(List<ResultCategory> list, ResultCategory t) {
        List<ResultCategory> tlist = new ArrayList<ResultCategory>();
        Iterator<ResultCategory> it = list.iterator();
        while (it.hasNext())
        {
            ResultCategory n = (ResultCategory) it.next();
            if (n.getParentId().longValue() == t.getGroupId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
